﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Entities;
using Entities;
using IDAL;
using System.Collections.Generic;
public partial class _Default : System.Web.UI.Page 
{
    public int AddOrUpdateSign = 0;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["AdminName"] == null)
            Response.Redirect("login.aspx");

        if (!Page.IsPostBack)
        {
            this.cblUsers.Items.Clear();
            DAL.UserDALs userdals = new DAL.UserDALs();
            this.cblUsers.DataSource = userdals.GetList();
            this.cblUsers.DataBind();

            this.gdvList.DataSource = BLL.BGroup.GetMyList(0);

            string[] keys ={ "GroupId" };
            gdvList.DataKeyNames = keys;
            this.gdvList.DataBind();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(txtName.Text))
        {
            return;
        }
        Group group = new Group();
        //XMLDAL.Provider<Entity.Group> pro = new XMLDAL.Provider<Entity.Group>();
        //Entity.Group group = new Entity.Group();
        group.Name = txtName.Text.Trim();
     
        BLL.BGroup.Insert(group);
        //用户列表用以保存
        //Provider<User> user = new Provider<User>();
        List<User> users = new List<User>();
        foreach (ListItem item in this.cblUsers.Items)
        {
            if (item.Selected)
            {
                User u = new User();
                u.User_id = Convert.ToInt16(item.Value);
                users.Add(u);
            }
        }
        if (users.Count > 0)
        {
            BLL.BGroup.InsertUsers(group.GroupID, users);
        }
        Response.Redirect("Group.aspx");
    }

    protected void gdvList_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int id = Convert.ToInt32(gdvList.DataKeys[e.NewEditIndex].Value);
        Group group = BLL.BGroup.Load(id);

        this.txtName.Text = group.Name;
        
        this.cblUsers.Items.Clear();
        DAL.UserDALs userdals = new DAL.UserDALs();
        this.cblUsers.DataSource = userdals.GetList();
        this.cblUsers.DataBind();

        DAL.GroupUserDALs groupuserdals = new DAL.GroupUserDALs();
        IList<GroupUser> lst = groupuserdals.GetByGroupId(id);

        foreach (GroupUser groupuser in lst)
        {
            foreach (ListItem item in cblUsers.Items)
            {
                if (groupuser.UserID.ToString() == item.Value)
                {
                    item.Selected = true;
                }
            }

        }

        this.gdvList.DataSource = BLL.BGroup.GetMyList(0);
        this.gdvList.DataBind();

        this.HidGroupId.Value = id.ToString();
        AddOrUpdateSign = 1;
        
    }

    protected void gdvList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = Convert.ToInt32(gdvList.DataKeys[e.RowIndex].Value);
        Group group  = BLL.BGroup.Load(id);
        DAL.GroupDALs groupdals = new DAL.GroupDALs();
        groupdals.DeleteById(id);
        DAL.GroupUserDALs groupuserdals = new DAL.GroupUserDALs();
        groupuserdals.DeleteByGroupId(id);
        Response.Redirect("Group.aspx");
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(this.HidGroupId.Value);
        Group group = BLL.BGroup.Load(id);
        group.Name = this.txtName.Text;
        BLL.BGroup.Update(group);

        List<User> users = new List<User>();
        foreach (ListItem item in this.cblUsers.Items)
        {
            if (item.Selected)
            {
                User u = new User();
                u.User_id = Convert.ToInt16(item.Value);
                users.Add(u);
            }
        }
        if (users.Count > 0)
        {
            BLL.BGroup.DeleteUsers(group.GroupID);
            BLL.BGroup.InsertUsers(group.GroupID, users);
        }
        Response.Redirect("Group.aspx");

    }

    protected void gdvList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gdvList.PageIndex = e.NewPageIndex;
        
        this.cblUsers.Items.Clear();
        DAL.UserDALs userdals = new DAL.UserDALs();
        this.cblUsers.DataSource = userdals.GetList();
        this.cblUsers.DataBind();

        this.gdvList.DataSource = BLL.BGroup.GetMyList(0);

        string[] keys ={ "GroupId" };
        gdvList.DataKeyNames = keys;
        this.gdvList.DataBind();
    }
}
